package com.qd.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.domain.entity.station.*;
import com.qd.common.sys.enums.OperatorType;
import com.qd.system.service.station.PersonStationOrgService;
import com.qd.system.service.station.StationOrgService;
import com.qd.system.service.station.StationSetService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 岗位职责
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys")
public class StationSetController {

    private final StationSetService stationSetService;
    private final StationOrgService stationOrgService;
    private final PersonStationOrgService personStationOrgService;

    /**
     * 添加岗位
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:save')")
    @OperatorInfo(title = "添加岗位", operator = OperatorType.INSERT)
    @PostMapping(value = "/station/save")
    public StationSetDTO save(@Validated({DefaultGroup.class}) @RequestBody StationSetDTO dto) {
        if (Objects.isNull(dto.getAddTime())) {
            dto.setAddTime(LocalDateTime.now());
        }
        return stationSetService.save(dto);
    }

    /**
     * 修改岗位
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:edit')")
    @OperatorInfo(title = "修改岗位", operator = OperatorType.UPDATE)
    @PutMapping(value = "/station/update")
    public StationSetDTO update(@Validated({DefaultGroup.class, UpdateGroup.class}) @RequestBody StationSetDTO dto) {
        if (Objects.isNull(dto.getUpdateTime())) {
            dto.setUpdateTime(LocalDateTime.now());
        }
        return stationSetService.update(dto);
    }

    /**
     * 删除岗位
     *
     * @param delDTO 主键
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:delete')")
    @OperatorInfo(title = "删除岗位", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/station/delete")
    public ApiResultWrapper<Object> removeById(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            stationSetService.removeById(ids.get(0));
        } else {
            stationSetService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取岗位
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/station/getById")
    public StationSetDTO getById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return stationSetService.getByIdDto(id);
    }

    /**
     * 岗位列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/station/page")
    public IPage<StationSetDTO> page(StationSetDTO dto) {
        return stationSetService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 岗位导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:station:table:export')")
    @OperatorInfo(title = "岗位导出", operator = OperatorType.EXPORT)
    @PostMapping(value = "/station/export")
    public void export(@RequestBody(required = false) StationSetDTO query) {
        stationSetService.export(query);
    }

    /**
     * 添加岗位和机构的关系
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:addOrg')")
    @OperatorInfo(title = "添加岗位和机构的关系", operator = OperatorType.INSERT)
    @PostMapping(value = "/stationOrg/save")
    public ApiResultWrapper<Object> stationOrgSave(@Validated({DefaultGroup.class}) @RequestBody StationOrgAllDTO dto) {
        stationOrgService.saveAll(dto);
        return ApiResult.success();
    }

    /**
     * 删除岗位和机构的关系
     *
     * @param delDTO 主键
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:deleteOrg')")
    @OperatorInfo(title = "删除岗位和机构的关系", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/stationOrg/delete")
    public ApiResultWrapper<Object> stationOrgDelete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            stationOrgService.removeById(ids.get(0));
        } else {
            stationOrgService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取岗位和机构的关系
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/stationOrg/getById")
    public StationOrgDTO getStationOrgById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return stationOrgService.getByIdDto(id);
    }

    /**
     * 岗位和机构的关系列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/stationOrg/page")
    public IPage<StationOrgDTO> stationOrgPage(StationOrgDTO dto) {
        return stationOrgService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 导出岗位和机构的关系
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:station:exportOrg')")
    @OperatorInfo(title = "导出岗位和机构的关系", operator = OperatorType.EXPORT)
    @GetMapping(value = "/stationOrg/export")
    public void stationOrgExport(@RequestBody(required = false) StationOrgDTO query) {
        stationOrgService.export(query);
    }

    /**
     * 添加岗位机构和人员关系
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:addOrgPerson')")
    @OperatorInfo(title = "添加岗位机构和人员关系", operator = OperatorType.INSERT)
    @PostMapping(value = "/stationOrgPerson/save")
    public ApiResultWrapper<Object> stationOrgPersonSave(@Validated({DefaultGroup.class}) @RequestBody PersonStationOrgListDTO dto) {
        personStationOrgService.saveBatch(dto);
        return ApiResult.success();
    }

    /**
     * 删除岗位机构和人员关系
     *
     * @param delDTO 的主键，多个用分号隔开
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:station:deleteOrgPerson')")
    @OperatorInfo(title = "删除岗位机构和人员关系", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/stationOrgPerson/delete")
    public ApiResultWrapper<Object> stationOrgPersonDelete(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            personStationOrgService.removeById(ids.get(0));
        } else {
            personStationOrgService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 获取岗位机构和人员关系
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/stationOrgPerson/getById")
    public PersonStationOrgDTO getStationOrgPersonById(@Validated @NotBlank(message = DelDTO.ID_NULL_MSG) String id) {
        return personStationOrgService.getByIdDto(id);
    }

    /**
     * 岗位机构和人员关系列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/stationOrgPerson/page")
    public IPage<PersonStationOrgDTO> stationOrgPersonPage(PersonStationOrgDTO dto) {
        return personStationOrgService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 导出岗位机构和人员关系
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:station:exportOrgPerson')")
    @OperatorInfo(title = "导出岗位机构和人员关系", operator = OperatorType.EXPORT)
    @GetMapping(value = "/stationOrgPerson/export")
    public void stationOrgPersonExport(@RequestBody(required = false) PersonStationOrgDTO query) {
        personStationOrgService.export(query);
    }
}